using System.Collections.Generic;

namespace DanMuHelper.Pool
{

    /// <summary>
    /// List缓存
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public static class ListPool<T>
    {
        static Queue<List<T>> _pool;

        public static List<T> Create()
        {
            List<T> ret = null;
            if (null != _pool && _pool.Count > 0)
            {
                ret = _pool.Dequeue();
            }
            if (null == ret)
            {
                ret = new List<T>();
            }
            return ret;
        }

        public static void Release(ref List<T> target)
        {
            // TODO: 检测target是否是从缓存池中创建的
            // TODO: 缓存池上限
            var tmp = target;
            target = null;
            if (!System.Object.ReferenceEquals(tmp, null))
            {
                tmp.Clear();
                if (null == _pool)
                {
                    _pool = new Queue<List<T>>();
                }
                _pool.Enqueue(tmp);
            }
        }

        public static void ClearPool()
        {
            if (null != _pool)
            {
                _pool.Clear();
            }
        }

        public static void ResetPool()
        {
            _pool = null;
        }
    }

}

